package com.bishe.tax.service;

import com.bishe.tax.entity.ChangeTaxRegistration;
import com.bishe.tax.entity.SysUser;
import com.bishe.tax.entity.TaxRegistration;
import com.bishe.tax.mapper.ChangeTaxRegistrationMapper;
import com.bishe.tax.mapper.TaxRegistrationMapper;
import com.bishe.tax.utils.AjaxResult;
import com.bishe.tax.utils.Convert;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 税务登记 服务层实现
 *
 * @author ruoyi
 * @date 2019-06-23
 */
@Service
public class TaxRegistrationService {
    @Autowired
    private TaxRegistrationMapper registrationMapper;
    @Autowired
    private ChangeTaxRegistrationMapper changeRegistrationMapper;
    @Autowired
    IdentityService identityservice;
    @Autowired
    RuntimeService runtimeservice;

    /**
     * 查询税务登记信息
     *
     * @param txId 税务登记ID
     * @return 税务登记信息
     */

    public TaxRegistration selectRegistrationById(Integer txId) {
        return registrationMapper.selectRegistrationById(txId);
    }

    /**
     * 查询税务登记列表
     *
     * @param registration 税务登记信息
     * @return 税务登记集合
     */

    public List<TaxRegistration> selectRegistrationList(TaxRegistration registration) {
        return registrationMapper.selectRegistrationList(registration);
    }

    /**
     * 新增税务登记
     *
     * @param registration 税务登记信息
     * @return 结果
     */

    public int insertRegistration(TaxRegistration registration) {
        registration.setStatus(1);
        return registrationMapper.insertRegistration(registration);
    }

    /**
     * 修改税务登记
     *
     * @param registration 税务登记信息
     * @return 结果
     */

    public int updateRegistration(TaxRegistration registration) {
        return registrationMapper.updateRegistration(registration);
    }

    /**
     * 删除税务登记对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */

    public int deleteRegistrationByIds(String ids) {
        return registrationMapper.deleteRegistrationByIds(Convert.toStrArray(ids));
    }

    public int doSubmitFlow(SysUser user, String txId) {
        TaxRegistration registration = registrationMapper.selectRegistrationById(Integer.valueOf(txId));
        Map<String, Object> variables = new HashMap<>();
        String creator = user.getUserId().toString();
        variables.put("creator", creator);
        variables.put("type", "taxRegistration");
        identityservice.setAuthenticatedUserId(user.getUserId().toString());
        ProcessInstance instance = runtimeservice.startProcessInstanceByKey("singleExamine",
                registration.getTxId().toString(), variables);
        registration.setProcessInstanceId(instance.getId());
        registration.setStatus(2);
        return registrationMapper.updateRegistration(registration);
    }

    public int doSubmitChangeFlow(SysUser user, TaxRegistration registration) {
        // 临时表增加
        ChangeTaxRegistration changeTaxRegistration = new ChangeTaxRegistration();
        BeanUtils.copyProperties(registration, changeTaxRegistration);
        //走流程
        Map<String, Object> variables = new HashMap<>();
        String creator = user.getUserId().toString();
        variables.put("creator", creator);
        variables.put("type", "taxRegistrationChange");
        identityservice.setAuthenticatedUserId(user.getUserId().toString());
        ProcessInstance instance = runtimeservice.startProcessInstanceByKey("singleExamine",
                registration.getTxId().toString(), variables);
        //保存登记
        registration =  registrationMapper.selectRegistrationById(registration.getTxId());
        registration.setProcessInstanceId(instance.getId());
        registration.setStatus(5);
        changeTaxRegistration.setStatus(5);
        changeRegistrationMapper.insertRegistration(changeTaxRegistration);
        return registrationMapper.updateRegistration(registration);
    }

    public AjaxResult updateChange(TaxRegistration registration,Boolean flag) {
        ChangeTaxRegistration changeTaxRegistration = changeRegistrationMapper.selectRegistrationById(registration.getTxId());
        if (changeTaxRegistration!=null){
            registration =  registrationMapper.selectRegistrationById(registration.getTxId());
            if (flag) {
                BeanUtils.copyProperties(changeTaxRegistration,registration,"status","infoId","processInstanceId");
            } else {
                registration.setStatus(6);
            }
            registrationMapper.updateRegistration(registration);
            changeRegistrationMapper.deleteRegistrationById(registration.getTxId());
            return AjaxResult.success();
        }
        return AjaxResult.error();
    }



    public int doSubmitDeleteFlow(SysUser user, TaxRegistration registration) {
        Map<String, Object> variables = new HashMap<>();
        String creator = user.getUserId().toString();
        variables.put("creator", creator);
        variables.put("type", "taxRegistrationDelete");
        identityservice.setAuthenticatedUserId(user.getUserId().toString());
        ProcessInstance instance = runtimeservice.startProcessInstanceByKey("deleteExamine",
                registration.getTxId().toString(), variables);
        registration.setProcessInstanceId(instance.getId());
        registration.setStatus(8);
        return registrationMapper.updateRegistration(registration);
    }

    public List<TaxRegistration> selectRegistrationListByChoosen(TaxRegistration registration) {
        return registrationMapper.selectRegistrationListByChoosen(registration);
    }


    /**
     * 查询税务登记信息
     *
     * @param txId 税务登记ID
     * @return 税务登记信息
     */

    public ChangeTaxRegistration selectChangeRegistrationById(Integer txId) {
        return changeRegistrationMapper.selectRegistrationById(txId);
    }


}
